﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERP.Page;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class DistrictsUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDr();
            }
        }

        private void BindDr()
        {
            var clsTinh = new Province_BLL();
            var dtTinh = clsTinh.BindGirdProvince(string.Empty);
            if (dtTinh != null && dtTinh.Rows.Count > 0)
            {
                drTinh.Items.Clear();
                drTinh.Items.Add(new ListItem("-Vui lòng chọn tỉnh-", "-1"));
                drTinh.DataSource = dtTinh;
                drTinh.DataValueField = "Province_Id";
                drTinh.DataTextField = "Province_Domain";
                drTinh.DataBind();
            }
        }

        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertDistricts();
            }
            else
            {
                UpdateDistricts();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {
            BindDr();
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsDistricts = new Districts_BLL();
            var dtDistricts = clsDistricts.BindGirdDistricts(string.Empty);
            if (dtDistricts != null && dtDistricts.Rows.Count > 0)
            {
                foreach (DataRow dr in dtDistricts.Rows)
                {
                    if (txtMaQuanHuyen.Text == dr["Districts_Alias"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã quận huyện !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                    else if (txtTenQuanHuyen.Text == dr["Districts_Name"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng tên quận huyện !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertDistricts()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Districts_ET
                    {
                        Districts_Alias = txtMaQuanHuyen.Text,
                        Districts_Name = txtTenQuanHuyen.Text,
                        Province_Id = int.Parse(drTinh.SelectedValue),
                        Districts_Note = txtGhiChu.Text
                    };
                    Districts_BLL.InsertDistricts(obj);
                    ClearTextBox();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaQuanHuyen.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateDistricts()
        {
            try
            {
                var obj = new Districts_ET
                {
                    Districts_Id = int.Parse(lbID.Value),
                    Districts_Alias = txtMaQuanHuyen.Text,
                    Districts_Name = txtTenQuanHuyen.Text,
                    Province_Id = int.Parse(drTinh.SelectedValue),
                    Districts_Note = txtGhiChu.Text
                };
                Districts_BLL.UpdateDistricts(obj);
                ((District)Page).GetDistricts();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaQuanHuyen.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaQuanHuyen.Text = string.Empty;
            txtTenQuanHuyen.Text = string.Empty;
            drTinh.SelectedIndex = 0;
            txtGhiChu.Text = string.Empty;
        }
        #endregion
    }
}